package com.lhy.admin.common.mvcplus.controller;


import com.lhy.admin.Constants;
import com.lhy.admin.common.mvcplus.entity.ReqCriteria;
import com.lhy.admin.common.mvcplus.entity.RespPage;
import com.lhy.admin.common.mvcplus.entity.RespResult;
import com.lhy.admin.common.mvcplus.entity.SearchCriteria;
import com.lhy.admin.common.mvcplus.service.SimpleService;
import com.lhy.admin.util.ReflectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * SimpleCrudControllerSupport<br/>
 * 简易controller支持类，提供了常用的增删改查方法<br/>
 * controller 可以继承该类
 *
 * @author hyluan
 */
public abstract class SimpleCrudControllerSupport<S extends SimpleService, T extends Serializable, P extends Serializable>
        implements SimpleCrudController<T, P> {

    @Autowired
    protected S service;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Override
    public RespResult<RespPage<T>> searchCriteria(@RequestBody ReqCriteria criteriaReq) {
        RespResult<RespPage<T>> ok = RespResult.ok();
        return ok.setData((RespPage<T>) service.searchCriteria(criteriaReq));
    }

    @Override
    public RespResult<List<T>> queryCond(@Validated @ModelAttribute T bean) {
        return RespResult.ok((List<T>) service.list(bean));
    }

    @Override
    public RespResult<T> get(@PathVariable("id") P id) {
        return RespResult.ok((T) service.getById(id));
    }

    @Override
    public RespResult<T> getCond(@Validated @ModelAttribute T bean) {
        Optional<T> one = service.getOne(bean);
        return RespResult.ok(one.orElse(null));
    }

    @Override
    public RespResult<Object> create(@RequestBody T bean) {
        return RespResult.create(service.save(bean));
    }

    @Override
    public RespResult<Object> update(@RequestBody T bean) {
        return RespResult.create(service.updateSelectiveById(bean))
                .setData(ReflectionUtils.getFieldValue(bean, Constants.PRIMARY_KEY));
    }

    @Override
    public RespResult<Object> delete(@PathVariable("id") P id) {
        return RespResult.create(service.removeById(id));
    }

    @Override
    public RespResult<Object> delete(@RequestParam("items") Set<P> items) {
        return RespResult.create(service.removeByIds(items));
    }

    @Override
    public RespResult<Object> deleteCond(@Validated @ModelAttribute T bean) {
        return RespResult.create(service.remove(bean));
    }

    @Override
    public RespResult<Object> validate(T bean) {
        return RespResult.ok(!service.exist(bean));
    }


    @Override
    public RespResult<Object> createBatch(List<T> beans) {
        return RespResult.create(service.saveBatch(beans));

    }

    @Override
    public RespResult<Object> deleteCriteria(SearchCriteria searchCriteria) {
        return RespResult.create(service.deleteCriteria(searchCriteria));
    }

    @Override
    public RespResult<Long> countCriteria(SearchCriteria searchCriteria) {
        return RespResult.<Long>ok().setData(service.countCriteria(searchCriteria));
    }

    @Override
    public RespResult<List<T>> listByIds(Set items) {
        return RespResult.ok((List<T>) service.listByIds(items));
    }

    @Override
    public RespResult<Object> updateBatch(List<T> beans) {
        return RespResult.create(service.updateBatchById(beans));
    }
}
